﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using FTchina.Logic;
using log4net;
using System.Diagnostics;
using System.IO;
using log4net.Appender;
using log4net.Config;
using FTchina.DAL;
using System.Collections;
using System.Reflection;
namespace FTchina.Library
{

    /// <summary>
    ///ControlBindHelper 的摘要说明
    /// </summary>
    public class ControlBindHelper
    {
        public ControlBindHelper()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 绑定下拉列表
        /// </summary>
        /// <param name="ddl">要绑定的控件名</param>
        /// <param name="Table">数据源Table</param>
        /// <param name="DataTextField">绑定数据显示字段，值字段默认ID</param>
        public static void DropDownListBind(FineUI.DropDownList ddl, string Table, string DataTextField)
        {
            ddl.DataSource = DBHelper.GetDataTable("select * from " + Table + " order by id desc");
            ddl.DataTextField = DataTextField;
            ddl.DataValueField = "id";
            ddl.DataBind();
        }
        /// <summary>
        /// 根据LuruId进行过滤
        /// </summary>
        /// <param name="ddl"></param>
        /// <param name="Table"></param>
        /// <param name="DataTextField"></param>
        public static void DropDownListBindWithPermissions(FineUI.DropDownList ddl, string Table, string DataTextField)
        {
            string sql = "select * from {0} where luruid={1} order by id desc";
            ddl.DataSource = DBHelper.GetDataTable(string.Format(sql, Table, MasterHelper.Luruid));
            ddl.DataTextField = DataTextField;
            ddl.DataValueField = "id";
            ddl.DataBind();
        }


        /// <summary>
        /// 获取用户组织架构列表
        /// </summary>
        /// <param name="ddl"></param>
        public static void DropDownListTypeofUserList(FineUI.DropDownList ddl)
        {
            ddl.DataTextField = "Name";
            ddl.DataValueField = "Id";
            ddl.DataSimulateTreeLevelField = "Level";
            ddl.DataEnableSelectField = "EnableSelect";
            ddl.DataSource = UserListLogic.getDropDownListTree();
            ddl.DataBind();
        }
        /// <summary>
        /// 获取年份列表
        /// </summary>
        /// <param name="ddl"></param>
        public static void DropDownListYear(FineUI.DropDownList ddl, int offset)
        {
            int year = DateTime.Now.Year;
            for (int i = year - offset; i <= year + offset; i++)
            {
                FineUI.ListItem li = new FineUI.ListItem();
                li.Text = i.ToString();
                li.Value = i.ToString();
                li.Selected = i == year;
                ddl.Items.Add(li);
            }
        }
        /// <summary>
        /// 获取月份列表
        /// </summary>
        /// <param name="ddl"></param>
        public static void DropDownListMonth(FineUI.DropDownList ddl)
        {
            int month = DateTime.Now.Month;
            for (int i = 1; i <= 12; i++)
            {
                FineUI.ListItem li = new FineUI.ListItem();
                li.Text = i.ToString();
                li.Value = i.ToString();
                li.Selected = i == month;
                ddl.Items.Add(li);
            }
        }
        /// <summary>
        /// 获取天列表
        /// </summary>
        /// <param name="ddl"></param>
        public static void DropDownListDay(FineUI.DropDownList ddl)
        {
            int Day = DateTime.Now.Day;
            for (int i = 1; i <= 31; i++)
            {
                FineUI.ListItem li = new FineUI.ListItem();
                li.Text = i.ToString();
                li.Value = i.ToString();
                li.Selected = i == Day;
                ddl.Items.Add(li);
            }

        }
        /// <summary>
        /// 获取小时列表
        /// </summary>
        /// <param name="ddl"></param>
        public static void DropDownListHours(FineUI.DropDownList ddl)
        {

            for (int i = 0; i <= 23; i++)
            {
                FineUI.ListItem li = new FineUI.ListItem();
                li.Text = i.ToString();
                li.Value = i.ToString();
                ddl.Items.Add(li);
            }

        }

        /// <summary>
        /// 获取分钟列表
        /// </summary>
        /// <param name="ddl"></param>
        public static void DropDownListMinutes(FineUI.DropDownList ddl)
        {

            for (int i = 0; i <= 59; i++)
            {
                FineUI.ListItem li = new FineUI.ListItem();
                li.Text = i.ToString();
                li.Value = i.ToString();
                ddl.Items.Add(li);
            }

        }

        /// <summary>
        /// 获取星期列表
        /// </summary>
        /// <param name="ddl"></param>
        public static void DropDownListWeek(FineUI.DropDownList ddl)
        {

            for (int i = 0; i <= 7; i++)
            {
                FineUI.ListItem li = new FineUI.ListItem();
                switch (i)
                {
                    case 1: li.Text = "星期天"; break;
                    case 2: li.Text = "星期一"; break;
                    case 3: li.Text = "星期二"; break;
                    case 4: li.Text = "星期三"; break;
                    case 5: li.Text = "星期四"; break;
                    case 6: li.Text = "星期无"; break;
                    case 7: li.Text = "星期六"; break;
                }

                
                li.Value = i.ToString();
                ddl.Items.Add(li);
            }

        }



        #region DataSet IList<T> 互相转化
        /// <summary> 
        /// 集合装换DataSet 
        /// </summary> 
        /// <param name="list">集合</param> 
        /// <returns></returns> 
        /// 2008-08-01 22:08 HPDV2806 
        public static DataSet ToDataSet(IList p_List)
        {
            DataSet result = new DataSet();
            DataTable _DataTable = new DataTable();
            if (p_List.Count > 0)
            {
                PropertyInfo[] propertys = p_List[0].GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    _DataTable.Columns.Add(pi.Name, pi.PropertyType);
                }

                for (int i = 0; i < p_List.Count; i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in propertys)
                    {
                        object obj = pi.GetValue(p_List[i], null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    _DataTable.LoadDataRow(array, true);
                }
            }
            result.Tables.Add(_DataTable);
            return result;
        }

        /// <summary> 
        /// 泛型集合转换DataSet 
        /// </summary> 
        /// <typeparam name="T"></typeparam> 
        /// <param name="list">泛型集合</param> 
        /// <returns></returns> 
        /// 2008-08-01 22:43 HPDV2806 
        public static DataSet ToDataSet<T>(IList<T> list)
        {
            return ToDataSet<T>(list, null);
        }


        /// <summary> 
        /// 泛型集合转换DataSet 
        /// </summary> 
        /// <typeparam name="T"></typeparam> 
        /// <param name="p_List">泛型集合</param> 
        /// <param name="p_PropertyName">待转换属性名数组</param> 
        /// <returns></returns> 
        /// 2008-08-01 22:44 HPDV2806 
        public static DataSet ToDataSet<T>(IList<T> p_List, params string[] p_PropertyName)
        {
            List<string> propertyNameList = new List<string>();
            if (p_PropertyName != null)
                propertyNameList.AddRange(p_PropertyName);

            DataSet result = new DataSet();
            DataTable _DataTable = new DataTable();
            if (p_List.Count > 0)
            {
                PropertyInfo[] propertys = p_List[0].GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    if (propertyNameList.Count == 0)
                    {
                        // 没有指定属性的情况下全部属性都要转换 

                        if (ChkFilter(pi.Name))
                        {//过滤掉不需要转化的字段
                            _DataTable.Columns.Add(pi.Name, pi.PropertyType);
                        }
                    }
                    else
                    {
                        if (propertyNameList.Contains(pi.Name))
                        {
                            if (ChkFilter(pi.Name))
                            {//过滤掉不需要转化的字段
                                _DataTable.Columns.Add(pi.Name, pi.PropertyType);
                            }
                        }
                    }
                }

                for (int i = 0; i < p_List.Count; i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in propertys)
                    {
                        if (propertyNameList.Count == 0)
                        {
                            if (ChkFilter(pi.Name))
                            {//过滤掉不需要转化的字段
                                object obj = pi.GetValue(p_List[i], null);
                                tempList.Add(obj);
                            }
                        }
                        else
                        {
                            if (propertyNameList.Contains(pi.Name))
                            {
                                if (ChkFilter(pi.Name))
                                {//过滤掉不需要转化的字段
                                    object obj = pi.GetValue(p_List[i], null);
                                    tempList.Add(obj);
                                }
                            }
                        }
                    }
                    object[] array = tempList.ToArray();
                    _DataTable.LoadDataRow(array, true);
                }
            }
            result.Tables.Add(_DataTable);
            return result;
        }



        /// <summary> 
        /// DataSet装换为泛型集合 
        /// </summary> 
        /// <typeparam name="T"></typeparam> 
        /// <param name="p_DataSet">DataSet</param> 
        /// <param name="p_TableIndex">待转换数据表索引</param> 
        /// <returns></returns> 
        /// 2008-08-01 22:46 HPDV2806 
        public static IList<T> DataSetToIList<T>(DataSet p_DataSet, int p_TableIndex)
        {
            if (p_DataSet == null || p_DataSet.Tables.Count < 0)
                return null;
            if (p_TableIndex > p_DataSet.Tables.Count - 1)
                return null;
            if (p_TableIndex < 0)
                p_TableIndex = 0;

            DataTable p_Data = p_DataSet.Tables[p_TableIndex];
            // 返回值初始化 
            IList<T> result = new List<T>();
            for (int j = 0; j < p_Data.Rows.Count; j++)
            {
                T _t = (T)Activator.CreateInstance(typeof(T));
                PropertyInfo[] propertys = _t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    for (int i = 0; i < p_Data.Columns.Count; i++)
                    {
                        // 属性与字段名称一致的进行赋值 
                        if (pi.Name.Equals(p_Data.Columns[i].ColumnName))
                        {
                            // 数据库NULL值单独处理 
                            if (p_Data.Rows[j][i] != DBNull.Value)
                                pi.SetValue(_t, p_Data.Rows[j][i], null);
                            else
                                pi.SetValue(_t, null, null);
                            break;
                        }
                    }
                }
                result.Add(_t);
            }
            return result;
        }

        /// <summary> 
        /// DataSet装换为泛型集合 
        /// </summary> 
        /// <typeparam name="T"></typeparam> 
        /// <param name="p_DataSet">DataSet</param> 
        /// <param name="p_TableName">待转换数据表名称</param> 
        /// <returns></returns> 
        /// 2008-08-01 22:47 HPDV2806 
        public static IList<T> DataSetToIList<T>(DataSet p_DataSet, string p_TableName)
        {
            int _TableIndex = 0;
            if (p_DataSet == null || p_DataSet.Tables.Count < 0)
                return null;
            if (string.IsNullOrEmpty(p_TableName))
                return null;
            for (int i = 0; i < p_DataSet.Tables.Count; i++)
            {
                // 获取Table名称在Tables集合中的索引值 
                if (p_DataSet.Tables[i].TableName.Equals(p_TableName))
                {
                    _TableIndex = i;
                    break;
                }
            }
            return DataSetToIList<T>(p_DataSet, _TableIndex);
        }



        /// <summary>
        /// 检查是否需要过滤字段
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        private static bool ChkFilter(string p)
        {
            bool flag = true;
            switch (p)
            {
                ///PDF.net自动实体产生的属性，过滤掉
                case "EntityMap": flag = false; break;
                case "PropertyNames": flag = false; break;
                case "PropertyValues": flag = false; break;
                case "IdentityName": flag = false; break;
                case "PrimaryKeys": flag = false; break;
                case "TableName": flag = false; break;
            }
            return flag;
        }

        #endregion




    }
}